﻿#include "filemanager.h"
#include <QString>

#include <QDir>
#include <QDebug>
#include <QTime>
#include  "Config.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QMenu>
#include "MoveFileThread.h"
#include <QDir>
#include <QFileIconProvider>
#include "FileSystemModal.h"
FileManager::FileManager(QWidget *parent)
	: QMainWindow(parent)
{
	
	ui.setupUi(this);

	initialVals();
	initialMenu();
	initialToolBar();
	initialFileSysModal();
	initialPathModal();
}

FileManager::~FileManager()
{
}
void FileManager::initialVals()
{
	reader_.getPathInfos(vec_path_);
	cmenu_ = new QMenu(ui.tableView);
	thread_ = new MoveFileThread();
}
void FileManager::initialMenu()
{
	ui.menuBar->hide();
}
void FileManager::initialToolBar()
{
	QAction* openDir = ui.mainToolBar->addAction(QIcon("img/open_dir.png"),"open");
	connect(openDir, SIGNAL(triggered()), this, SLOT(onOpenFolder())); 
	ui.mainToolBar->setContextMenuPolicy(Qt::PreventContextMenu);
}

void FileManager::initialFileSysModal()
{
	

	filesys_modal_=new FileSystemModal();
	filesys_modal_->setHorizontalHeaderItem(TableConfig::CN_FILE_NAME,new QStandardItem(QObject::tr("Name")));
	filesys_modal_->setHorizontalHeaderItem(TableConfig::CN_LAST_MODIFY_TIME,new QStandardItem(QObject::tr("LastAccessTime")));
	filesys_modal_->setHorizontalHeaderItem(TableConfig::CN_CREATE_TIME,new QStandardItem(QObject::tr("CreateTime")));
	filesys_modal_->setHorizontalHeaderItem(TableConfig::CN_AB_PATH,new QStandardItem(QObject::tr("AbsolutePath")));
	filesys_modal_->setHorizontalHeaderItem(TableConfig::CN_EXT,new QStandardItem(QObject::tr("Ext")));
	ui.tableView->setModel(filesys_modal_);

	ui.tableView->setContextMenuPolicy(Qt::CustomContextMenu);
	connect(ui.tableView,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(onContextMenuPaths(QPoint))); 

	ui.tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
	//设置表格的单元为只读属性，即不能编辑
	ui.tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
	//设置排序方式
	filesys_modal_->sort(TableConfig::CN_FILE_NAME,Qt::DescendingOrder);
	//是否显示网格
	ui.tableView->setShowGrid(true);
	//设置QTableView的默认行高
	ui.tableView->verticalHeader()->setDefaultSectionSize(18);

}
void FileManager::initialPathModal()
{
	paths_modal_ = new QStandardItemModel();
	paths_modal_->setHorizontalHeaderItem(PathTableConfig::CN_NAME,new QStandardItem(QObject::tr("name")));
	paths_modal_->setHorizontalHeaderItem(PathTableConfig::CN_PATH,new QStandardItem(QObject::tr("path")));
	ui.tableView1->setModel(paths_modal_);
	ui.tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
	
	filesys_modal_->sort(TableConfig::CN_FILE_NAME,Qt::DescendingOrder);
	ui.tableView->setShowGrid(true);
	ui.tableView->verticalHeader()->setDefaultSectionSize(18);	
	int nCount = 0;
	for (auto itor=vec_path_.begin();itor!=vec_path_.end();++itor)
	{
		paths_modal_->setItem(nCount,PathTableConfig::CN_NAME,new QStandardItem((*itor).name));
		paths_modal_->setItem(nCount,PathTableConfig::CN_PATH,new QStandardItem((*itor).path));
		++nCount;
	}
}


void FileManager::onOpenFolder()
{

	 QFileDialog dlg;  
	 cur_dir_path_ = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
		 "",
		 QFileDialog::ShowDirsOnly
		 | QFileDialog::DontResolveSymlinks);

	if (!cur_dir_path_.isEmpty())
	{
		filesys_modal_->setCurrentPath(cur_dir_path_);
	}
 	 qDebug()<< cur_dir_path_;  
}


void  FileManager::onMoveTo(const QString& path)
{
	QItemSelectionModel * pSelect = ui.tableView->selectionModel();
	QModelIndexList pModelList = pSelect->selectedIndexes();
	foreach(QModelIndex index,pModelList)
	{
		QVariant val =  index.data();	

		if(index.column()==TableConfig::CN_AB_PATH)	
		{
			qDebug()<<val<<" "<<path;
			QFileInfo fileInfo(val.toString());
			thread_->setFromTo(val.toString(),QString(path).append(fileInfo.fileName()));
			//thread_->start();
			filesys_modal_->removeRows(index.row(),1);
		}
	}
	
}

void FileManager::onContextMenuPaths( QPoint point)
{
	cmenu_->clear();
	for (auto itor=vec_path_.begin();itor!=vec_path_.end();++itor)
	{
		QAction *AddRecode = cmenu_->addAction((*itor).name);//添加右键菜单显示项
		
		connect(AddRecode,&QAction::triggered,[=](){onMoveTo((*itor).path);});//使AddRecode与其他函数关联
	}
	cmenu_->exec(QCursor::pos());
}